
import { newFile, renameFile } from '@/api';
// import { useAppOpenEditor } from '@/hooks/useAppOpenEditor';
import useAppStore from '@ide/store';
import { useIdeStore } from 'ide-plugin';
import React, { useCallback, useEffect, useMemo } from 'react';
import { Button, Input, message } from 'tea-component';
import { Modal } from 'tea-component/lib/modal/Modal';

const FileModel = React.forwardRef(() => {
  const { setting } = useAppStore();
  const { addFileInfo, setAddFileInfo } = useAppStore();
  const { getFiles } = useAppStore();
  const { updateEditor, openEditor } = useIdeStore();
  // const { appOpenEditor } = useAppOpenEditor();
  useEffect(() => {
  }, [addFileInfo.isOpenAddFile]);
  const close = useCallback(() => {
    setAddFileInfo({
      isOpenAddFile: false
    });
  }, [setAddFileInfo]);
  const caption = useMemo(() => {
    const nameMap = {
      rename: '重命名',
      file: '新建文件',
      folder: '新建文件夹'
    };
    return addFileInfo.addFileType ? nameMap[addFileInfo.addFileType] : '';
  }, [addFileInfo]);
  const submit = useCallback(() => {
    if (addFileInfo.newFilePath && addFileInfo.newFileName) {
      if (addFileInfo.addFileType === 'rename') {
        const newPath = addFileInfo.newFilePath.replace(/[^/]+$/, addFileInfo.newFileName);
        renameFile({
          oldPath: addFileInfo.newFilePath,
          newPath
        }).then((res) => {
          if (res.code === 0) {
            getFiles();
            if (addFileInfo.newFilePath) {
              updateEditor(addFileInfo.newFilePath, {
                path: newPath,
                name: addFileInfo.newFileName
              });
            }
          }
          close();
        }).catch((e) => {
          message.error({ content: e.message });
        });
        // }else if(addFileInfo.addFileType==='contract'){
      } else {
        const path = addFileInfo.newFilePath + '/' + addFileInfo.newFileName;
        newFile({
          path,
          fileType: addFileInfo?.addFileType === 'file' ? 'f' : 'd'
        }).then(() => {
          getFiles().then(() => {
            if (addFileInfo?.addFileType === 'file') {
              openEditor({ path });
            }
          });
          close();
        }).catch((e) => {
          message.error({ content: e.message });
        });
      }
    }
  }, [addFileInfo, close]);
  return <Modal
    maskClosable
    visible={addFileInfo.isOpenAddFile}
    caption={caption}
    onClose={close}
    className={`${setting.theme}_model`}
    size='s'
    maskStyle={{
      backgroundColor: 'rgba(0,0,0,0.2)'
    }}
  >
    <Modal.Body>
      <Input value={addFileInfo.newFileName} size="full" onChange={(value) => setAddFileInfo({ newFileName: value })} />
    </Modal.Body>
    <Modal.Footer>
      <Button type="primary" disabled={!addFileInfo?.newFileName} onClick={submit}>
        确定
      </Button>
      <Button type="weak" onClick={close}>
        取消
      </Button>
    </Modal.Footer>
  </Modal>;
});
FileModel.displayName = 'FileModel';

export default FileModel;
